const { Router } = require('express')
const router = new Router()

// 引入分类模型（classification）
const ClassificationModel = require('../../../model/classification')

// 引入fs模块
const fs = require('fs')
// 引入path模块
const path = require('path')

const moveFile = require('../../../utils/fileUtils/moveFile')
// 验证权限
const rolesPermission = require('../../../utils/validateRole/rolesPermission')
// 引入url
const url = require('../../../config/url')


// 新增分类路由
router.get('/addClassification', function (request, response) {
    const { typeName, typePic, typeArr, isShow } = request.query

    // 需要移动的文件名
    const fileName = path.parse(typePic).base
    //  需要移动到的文件夹路径
    const newPath = path.resolve(__dirname,'../../../public/free/classification',fileName)
    // 临时存储的路径
    const oldPath = path.resolve(__dirname, '../../../public/free/temp', fileName)


    moveFile(newPath, oldPath, status => {
        const data = {
            code: '200',
            msg: '新增分类失败',
            data: 'error'
        }

        if (status.isSuccess){
            // 入库操作
            ClassificationModel.create({
                typeName,
                typePic: `${url}public/free/classification/${fileName}`,
                isShow,
                typeArr
            },(err, doc) => {
                if (err) {
                    console.log(err)
                    response.json(data)
                }else {
                    data.data = 'success'
                    data.msg = '新增分类成功'

                    response.json(data)
                }
            })
        } else {
            console.log(status.err)
            response.json(data)
        }
    })
})

router.get('/showClassification', function (request, response) {
    let count

    ClassificationModel.count({},function (err, doc) {
        if (err) return console.log(err);
        count = doc

        // 什么参数都不传表示拿到分类可以显示的数据
        const { page, limit, isShow } = request.query

        let option = {}

        // isShow：true 表示为展示可以显示的分类，false表示全部展示
        if (!page && !limit && isShow){
           return ClassificationModel.find({
               isShow: 0
           },{
               _v: 0,
               typePic: 0,
               _id: 0,
               typeName: 1,
               typeArr: 1
           },function (err,doc) {
               const data = {
                   code: '200',
                   data: 'error',
                   msg: '查询分类详情失败'
               }

               if (err) return response.json(data);

               data.data = doc1
               data.msg = '查询分类详情成功'
               data.count = count
               response.json(data)
           })
        }else if(isShow){
            option = {
                isShow: 0
            }
        }

        ClassificationModel.find( option,{
            _v: 0
        }).skip( (page - 1) * limit).limit(parseInt(limit)).exec(function (err,doc1) {
            const data = {
                code: '200',
                data: 'error',
                msg: '查询分类详情失败'
            }

            if (err) return response.json(data);

            data.data = doc1
            data.msg = '查询分类详情成功'
            data.count = count
            response.json(data)
        })

    })

})

module.exports = router
